<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 遍历对象 for in 
    let goods = {
      gname: "小米小米10 青春版",
      num: 100012816024,
      weight: "0.55kg",
      address: "中国大陆",
    }
    for (let key in goods) { // key 里面存的是字符串 "gname" "num" "weight" === key
      console.log(key);
      console.log(goods[key]);
      console.log(key + ":" + goods[key]);
    }

    let keys = Object.keys(goods); // Object.keys 直接获取属性名并返回一个数组
    console.log(keys);
    keys.forEach(key => {
      console.log(key);
      console.log(goods[key]);
      console.log(key + ":" + goods[key]);
    });

    let value = Object.values(goods); // Object.values 直接获取属性值并返回一个数组
    console.log(value);
    value.forEach(value => {
      console.log(value);
    });


    let entries = Object.entries(
      goods); // Object.entries 直接返回对象自身并可枚举对象键值对数组，数组中的每个元素都包含两个元素的数组，第一个是属性名key，第二个是属性值value
    entries.forEach(([key, value]) => {
      console.log(key + ":" + value);
    });

    // 可以用来遍历数组，但是不推荐
    // let arr = [1, 2, 3, 4, 5, 6, 7];
    // for (let k in arr) {
    //   console.log(arr[k]);
    // }
  </script>
</body>

</html>